package cn.anosi.service.base.impl;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;

import cn.anosi.page.PageResponseBean;
import cn.anosi.page.Pager;
import cn.anosi.repository.BaseRepository;
import cn.anosi.repository.auth.PermissionRepository;
import cn.anosi.repository.auth.RolePermissionRepository;
import cn.anosi.repository.auth.RoleRepository;
import cn.anosi.repository.auth.UserRepository;
import cn.anosi.repository.auth.UserRoleRepository;
import cn.anosi.service.base.BaseService;
import cn.anosi.shiro.CustomRealm;
import cn.anosi.utils.PageUtils;

@Transactional
public class BaseServiceImpl<T> implements BaseService<T> {

	private BaseRepository<T, Serializable> baseRepository;

	@Autowired
	protected UserRepository userRepository;

	@Autowired
	protected RoleRepository roleRepository;

	@Autowired
	protected PermissionRepository permissionRepository;

	@Autowired
	protected UserRoleRepository userRoleRepository;

	@Autowired
	protected RolePermissionRepository rolePermissionRepository;

	@Autowired
	protected CustomRealm customRealm;

	protected static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	protected static final SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd");

	@PersistenceContext
	protected EntityManager entityManager;

	protected Logger logger = null;

	protected BaseServiceImpl() {
		logger = LoggerFactory.getLogger(getClass().getName());
	}

	protected BaseServiceImpl(BaseRepository baseRepository) {
		logger = LoggerFactory.getLogger(getClass().getName());
		this.baseRepository = baseRepository;
	}

	@Override
	public T save(T entity) {
		return baseRepository.save(entity);
	}

	/**
	 * 单条删除
	 */
	@Override
	public int delete(Serializable id) {
		return baseRepository.deleteOne(id);
	}

	/**
	 * 单条查询
	 */
	@Override
	public T getById(Serializable id) {
		return baseRepository.getById(id);
	}

	/**
	 * 列表数据
	 * 
	 * @param pager
	 * @return
	 */
	@Override
	public PageResponseBean<T> list(Pager pager) {
		// 分页对象
		Page<T> datas = baseRepository.list(PageUtils.getPageable(pager));
		// 返回结果
		return PageUtils.getPageResponseBean(datas, pager);
	}

	/**
	 * 锁定
	 * 
	 * @param id
	 * @param status
	 * @return
	 */
	@Override
	public int lock(Serializable id, Integer status) {
		return baseRepository.lock(id, status);
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	public int batcahDel(List ids) {
		return baseRepository.batchDel(ids);
	}

	/**
	 * 列表
	 * 
	 * @return
	 */
	@Override
	public List<T> list() {
		return baseRepository.list();
	}

	/**
	 * 批量新增
	 * 
	 * @param list
	 * @return
	 */
	@Override
	public int batchSave(ArrayList<T> list) {
		List<T> datas = baseRepository.save(list);
		return datas.size();
	}

	/**
	 * 根据sql更新
	 */
	@Override
	public void updateBySql(String sql, Object... args) {
		Query query = entityManager.createNativeQuery(sql);
		int i = 0;
		for (Object arg : args) {
			query.setParameter(++i, arg);
		}
		query.executeUpdate();
	}

	/**
	 * 根据hql更新
	 */
	@Override
	public void updateByHql(String hql, Object... args) {
		Query query = entityManager.createQuery(hql);
		int i = 0;
		for (Object arg : args) {
			query.setParameter(++i, arg);
		}
		query.executeUpdate();
	}

	@Override
	public List<T> queryByHql(String hql, Object... args) {
		Query query = entityManager.createQuery(hql);
		int i = 0;
		for (Object arg : args) {
			query.setParameter(++i, arg);
		}
		return query.getResultList();
	}

	@Override
	public List<T> queryBySql(String sql, Object... args) {
		Query query = entityManager.createNamedQuery(sql);
		int i = 0;
		for (Object arg : args) {
			query.setParameter(++i, arg);
		}
		return query.getResultList();
	}
}
